package com.git.smp.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.codec.binary.Base64;

/**
 * 压缩/解压缩
 * @author sp.monk
 *
 */
public class StringCompress {
	/**
	 * 
	 * <p>
	 * Description:使用gzip进行解压缩
	 * </p>
	 * 
	 * @param compressedStr
	 * @return
	 */
	public static String gunzip(String compressedStr) {
		if (compressedStr == null) {
			return null;
		}

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = null;
		GZIPInputStream ginzip = null;
		byte[] compressed = null;
		String decompressed = null;
		try {
			//compressed = new sun.misc.BASE64Decoder()
			//		.decodeBuffer(compressedStr);
			compressed = Base64.decodeBase64(compressedStr);
			in = new ByteArrayInputStream(compressed);
			ginzip = new GZIPInputStream(in);

			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = ginzip.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString("GBK");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ginzip != null) {
				try {
					ginzip.close();
				} catch (IOException e) {
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}

		return decompressed;
	}

	/**
	 * 
	 * 使用gzip进行压缩
	 */
	public static String gzip(String primStr) {
		if (primStr == null || primStr.length() == 0) {
			return primStr;
		}

		ByteArrayOutputStream out = new ByteArrayOutputStream();

		GZIPOutputStream gzip = null;
		try {
			gzip = new GZIPOutputStream(out);
			gzip.write(primStr.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (gzip != null) {
				try {
					gzip.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return Base64.encodeBase64String(out.toByteArray());
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String msg = "H4sIAAAAAAAAAO2Yz27bMAzGX6XofTFJUX8YdAVWtBiGAX2HYgvWQ9sES1F0bz+qjgKrkuUdlsMGfZfE/GgQ0M+UaF/st3e7D5unl/XV9vuvs9fHh6f9OsU+nt8/P+/Ww7D/dr95vNuv1I7eavvzxxD/DJq0edjuNsP5JSKAISADDhnJMvDZKXQsYRGNnKTEf6o2nOSSBUSWyPKgVbL+tJCNNxDaFbhDBOHq5vOX2/HiRA/GPy9DznBjbcKIYxLJr6LkKzE6/2mp1htgAwRhhAsewQRvyszk07EYVLK6lJ/z6HDeJ2bUlpgw+4v8AoJoZ1eqHvwJv95/VTE5L4218SJMWYOWEeWHAt7pqhMIERr0gfE95sQPAcdMRam973xZ9egDHOh2fnUZ46yrdECSiFAA247U+BEWXDJ+8ZedcWjn9k/1J/xsmdWla8pWh46GL+LtQiTnpzOiQe0qLFb8ff+hbt3W+0r15Pf+WxITkXHzPmq75SdeGZnhF4paef85zSOEUCGT/M5vSRx0B2vML/qCF0+hZqTKT7h4KvL+QxUbAVtwHvsv+p3fkozikBY/kWCzAbWMVM8/lPb5FzM1N1iYnV/U7/wWxAGDVDrg6MfTDrAZmem/9vmX3h+o9j1l8v7Q+TVlxOucPu+jlRA4hFakPn8u7J+JnzeVyXLK7+b2+mLIvvhd/gbLWK9gARQAAA==";
		String b = StringCompress.gunzip(msg);
		//System.out.println(msg.getBytes().length);
		System.out.println(b);
		//System.out.println(b.getBytes().length);

		/**
		 * byte[] b = StringCompress.compress(msg);
		 * System.out.println(msg.getBytes().length); String bb = new String(b);
		 * System.out.println(bb); System.out.println(bb.getBytes().length);
		 */
	}
}
